<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018-04-25
 * Time: 21:31
 */

namespace app\cls\tom\log;


class FileLog extends LogBase
{
    public $logPath = "/data0/logs/tom";
    public $logFileName;
    public $isDayCut = true;

    public  function write()
    {
        if(empty($this->msg) && empty($this->detail)){
            return;
        }
        if(!file_exists($this->logPath)) {
            mkdir($this->logPath, 0777);
        }
        if(empty($this->logFileName)){
            $debugInfo = debug_backtrace();
            $this->logFileName = pathinfo($debugInfo[count($debugInfo)-1]["file"],PATHINFO_FILENAME);
        }else{
            $this->logFileName = pathinfo($this->logFileName,PATHINFO_FILENAME);
        }
        if($this->isDayCut){
            $date = date("Ymd");
            $this->logFileName = $this->logFileName."_".$date.".log";
        }else{
            $this->logFileName .= ".log";
        }
        $fullPath = $this->logPath.DIRECTORY_SEPARATOR.$this->logFileName;
        $time = "【".date("Y-m-d H:i:s")."】 ";
        $logType = "【 {$this->type} 】 ";
        $messageContent = implode(",",[trim($this->msg),trim($this->getDetail())]);
        $message = $time.$logType."【".$this->getKeyWordMsg()."】 ".$messageContent."\n";
        @file_put_contents($fullPath,$message,FILE_APPEND | LOCK_EX );
    }
}